<?php
namespace App\Admin\Controllers\Consultation;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use App\Admin\Controllers\Common\Common;

class NewClassController extends Controller {
	public function index() {
		return view('admin.consultation.newclass.list');
	}

	public function getSeaformOpt() {
		$campuses = DB::table('campuses')->get(['id','name']);
		$class_type = DB::table('class_types')->get(['id','name']);
		$class_stage = DB::table('class_section_groups')
			->where('pid','=',0)
			->get(['id','name']);
		$class_group = DB::table('class_section_groups')
			->where('pid','<>',0)
			->get(['id','name']);
		$tutor = DB::table('admin_users')->get(['id','username']);
		return ['code'=>1,'msg'=>'ok','data'=>[
			'campuses' => $campuses,
			'class_type' => $class_type,
			'class_stage' => $class_stage,
			'class_group' => $class_group,
			'tutor' => $tutor
		]];
	}

	public function getTableData(Request $r) {
		$offset = ($r['page']['pageNumber'] - 1) * $r['page']['pageSize'];
		$data = [];
		$data['rows'] = $this->mkWhereSql($r)
			->limit($r['page']['pageSize'])
			->offset($offset)
			->orderBy("n.".$r['sort']['sortBy'],"n.".$r['sort']['orderBy'])
			->get([
				'n.*','c.name as campus_name','t.name as type_name',
			]);
		$data['rows'] = json_decode(json_encode($data['rows']),true);
		foreach($data['rows'] as &$row) {
			$row['k'] = $row['id'];
			$row['stage_name'] = DB::table("class_section_groups")->where('id','=',$row['section'])->value('name');
			$row['group_name'] = DB::table("class_section_groups")->where('id','=',$row['group'])->value('name');
			$row['st_count'] = DB::table("consultation_st")->where('class_id','=',$row['id'])->count('id');
			$row['sex_rate'] = $row['male']." / ".$row['female'];
			$row['lvl_rate'] = $row['lvl_a']." / ".$row['lvl_b']." / ".$row['lvl_c']." / ".$row['lvl_d'];
		}
		$data['total'] = $this->mkWhereSql($r)->count();
		return ['code'=>1,'msg'=>'ok','data'=>$data];
	}
	private function mkWhereSql($r) {
		$db = DB::table('consultation_newclass as n')
			->leftJoin('campuses as c','n.campus','=','c.id')
			->leftJoin('class_types as t','n.type','=','t.id');
		if($r['year']){
			$db->where('n.year','=',$r['year']);
		}
		if($r['campus']){
			$db->where('n.campus','=',$r['campus']);
		}
		if($r['class_type']){
			$db->where('n.type','=',$r['class_type']);
		}
		if($r['class_stage']){
			$db->where('n.section','=',$r['class_stage']);
		}
		if($r['class_group']){
			$db->where('n.group','=',$r['class_group']);
		}
		if($r['no']){
			$db->where('n.no','=',$r['no']);
		}
		return $db;
	}

	public function create() {
		return view('admin.consultation.newclass.create');
	}

	public function store(Request $r) {
		DB::beginTransaction();
		try {
			$data = [
				'year' => $r['year'],
				'season' => $r['season'],
				'batch' => $r['batch'],
				'campus' => $r['campus'],
				'type' => $r['type'],
				'section' => $r['stage'],
				'group' => $r['group'],
				'no' => $r['no'],
				'tutor_id' => $r['tutor'],
				'tutor' => DB::table('admin_users')->where("id",'=',$r['tutor'])->value('username'),
				'total' => $r['total'],
				'male' => $r['male'],
				'female' => $r['female'],
				'lvl_a' => $r['lvl_a'],
				'lvl_b' => $r['lvl_b'],
				'lvl_c' => $r['lvl_c'],
				'lvl_d' => $r['lvl_d'],
			];
			DB::table("consultation_newclass")->insert($data);
			DB::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>"添加失败:".$e->getMessage()];
		}
	}

	/**
	 * [删除开班计划，还原咨询记录和学生状态]
	 */
	public function destory(Request $r) {
		DB::beginTransaction();
		try {
			// 删除开班计划
			DB::table('consultation_newclass')->whereIn('id',$r['ids'])->delete();
			// 还原咨询记录
				# code...
			// 还原学生状态
				# code...
			DB::commit();
			return ['code'=>1,'msg'=>'删除成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>"删除失败:".$e->getMessage()];
		}
	}

	public function edit($id) {
		$info = DB::table('consultation_newclass')->where('id','=',$id)->first();
		$info = json_decode(json_encode($info),true);
		return view("admin.consultation.newclass.edit",[
			"Info" => $info
		]);
	}

	public function update(Request $r) {
		DB::beginTransaction();
		try {
			$data = [
				'year' => $r['year'],
				'season' => $r['season'],
				'batch' => $r['batch'],
				'campus' => $r['campus'],
				'type' => $r['type'],
				'section' => $r['stage'],
				'group' => $r['group'],
				'no' => $r['no'],
				'tutor_id' => $r['tutor'],
				'tutor' => DB::table('admin_users')->where("id",'=',$r['tutor'])->value('username'),
				'total' => $r['total'],
				'male' => $r['male'],
				'female' => $r['female'],
				'lvl_a' => $r['lvl_a'],
				'lvl_b' => $r['lvl_b'],
				'lvl_c' => $r['lvl_c'],
				'lvl_d' => $r['lvl_d']
			];
			DB::table("consultation_newclass")->where('id','=',$r['id'])->update($data);
			DB::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>"添加失败:".$e->getMessage()];
		}
	}

	public function distribute(Request $r) {
		DB::beginTransaction();
		try {
			$list_ids = explode(",",$r['ids']);
			$st_ids = DB::table('consultation_list')->whereIn("id",$list_ids)->pluck("st_id");
			// 修改学生状态 和 class_id
			DB::table('consultation_st')->whereIn("id",$st_ids)->update([
				'class_id' => $r['newclass_id']
			]);
			// list状态改为已处理
			DB::table('consultation_list')->whereIn("id",$list_ids)->update([
				'status' => (int)11
			]);
			DB::commit();
			return ['code'=>1,'msg'=>'分配成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>"分配失败:".$e->getMessage()];
		}
	}
}
